<template>
    <el-dialog v-model="visible" title="导入数据" width="750" draggable overflow append-to-body>
        <el-tabs v-model="typeName" @click="handleCommand">
            <el-tab-pane name="wzhm" label="完整号码"> </el-tab-pane>
            <el-tab-pane name="aixh" label="AI 选号"> </el-tab-pane>
            <el-tab-pane name="xhc" label="选号池"> </el-tab-pane>
            <el-tab-pane name="gjsx" label="高级筛选"> </el-tab-pane>
        </el-tabs>
        <div class="mb-1" v-show="typeName === 'ai'">
            <AiProviderSelect v-model="aiProvider"></AiProviderSelect>
        </div>
        <el-input v-model="content" type="textarea" size="small" :rows="10" placeholder="请输入数据。"></el-input>
        <template #footer>
            <el-button-group size="small">
                <el-button type="primary" @click="onImport()">导入</el-button>
                <el-button type="danger" @click="onClear()">清空</el-button>
                <el-button type="info" @click="visible = false">关闭</el-button>
            </el-button-group>
        </template>
    </el-dialog>
</template>

<script setup>
import AiProviderSelect from '@/components/aiprovider-select/AiProviderSelect.vue';
import DltOrderModel from '@/model/DltOrderModel';
import { arrayUnique } from '@/utils/Utils';
import { split_index } from '@/config/dlt-config';
import { ElMessageBox } from 'element-plus'
import { ref } from 'vue';

let dltOrderModel = new DltOrderModel();
const content = ref('');
const visible = defineModel();
// // wzhm=完整号码, aixh=AI选号, xhc=选号池, gjsx=高级筛选
const typeName = ref('wzhm');
const aiProvider = ref('DeepSeek');

const emit = defineEmits(['submit']);

const onImport = () => {
    if (content.value.trim() === '') {
        return;
    }
    let poolColumns = [], poolAllText = '', list = [], filterInfo = {};
    switch (typeName.value) {
        case 'wzhm':
            list = content.value.replace(/\s{2,}/g, ' ').trim().split('\n').filter(v => v.trim().split(' ').length == 7);
            if (list.length > 0) {
                poolColumns = dltOrderModel.ordersToPoolData(list).map(v => v.join(' '));
                poolAllText = dltOrderModel.ordersToPoolData(list, false).map(v => v.join(' ')).join(', ');
            }
            break;
        case 'aixh':
            list = dltOrderModel.parseAiText(content.value.trim(), aiProvider.toLowerCase());
            if (list.length > 0) {
                poolColumns = dltOrderModel.ordersToPoolData(list).map(v => v.join(' '));
                poolAllText = dltOrderModel.ordersToPoolData(list, false).map(v => v.join(' ')).join(', ');
            }
            break;
        case 'xhc':
            poolColumns = content.value.trim().split('\n').map(v => v.trim().replace(/"|,/g, '').slice(3));
            poolAllText = `${arrayUnique(poolColumns.slice(0, split_index).join(' ').split(' ')).sort().join(' ')}, ${arrayUnique(poolColumns.slice(split_index).join(' ')).sort().join(' ')}`;
            break;
        case 'gjsx':
            filterInfo = JSON.parse(content.value.trim());
            break;
    }
    emit('submit', typeName.value, poolColumns, poolAllText, filterInfo, content.value.trim());
    content.value = '';
    visible.value = false;
}

const onClear = () => {
    ElMessageBox.confirm('确定要清空数据吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
    }).then(() => {
        content.value = '';
    })
}
const handleCommand = () => {
    switch (typeName.value) {
        case 'xhc':
            content.value = `"01": "01 02 03 04 07",
"02": "03 07 08 09 11",
"03": "06 12 16 17 19",
"04": "17 18 20 21 26 28",
"05": "23 24 27 33",
"06": "03 05 06 07",
"07": "05 07 09 10 11"`;
            return;
        case 'gjsx':
            content.value = `"sumRange": "86-90",
"trend": "下",
"frontRange": "11-28",
"backRange": "01-11",
"frontIncludes": "13 14 20 22 24 29 32",
"frontExcludes": "15 17 30 31 33",
"backIncludes": "06",
"backExcludes": "02 03",`;
            return;
        case 'wzhm':
            content.value = `03 11 18 27 34 05 09
07 14 21 25 33 02 08
01 09 16 30 35 06 12
05 12 19 22 28 03 10
08 13 20 26 31 04 07`;
            return;
        case 'aixh':
            content.value = `推荐号码组合
05 12 18 23 30 + 03 08
前区含冷号12、30，连号23-30跨度较大，后区奇偶搭配1318。
09 15 22 27 33 + 05 10
前区覆盖三区均衡分布，后区含近期高频热号101924。
02 07 14 25 31 + 04 09
前区含连号02-07，后区小号与中号组合1822。
11 16 21 26 35 + 06 11
前区全奇数组合，后区含热号061320。
04 10 17 24 29 + 02 07
前区含冷号04、29，后区小号为主1924。
08 13 19 28 32 + 01 06
前区跨度大，后区含冷号011822。
03 11 20 27 34 + 05 12
前区含连号27-34，后区全大号1320。
06 14 22 29 35 + 03 09
前区含热号22、35，后区奇偶平衡1924。
07 15 23 30 33 + 04 10
前区含连号23-30，后区含热号101822。
01 09 18 25 31 + 02 08
前区冷热号结合，后区小号与中号搭配1320。`;
            break;
    }
}

handleCommand();
</script>